package com.cdks.wuxiu.api.service;


import com.cdks.wuxiu.api.component.constant.Constant;
import com.cdks.wuxiu.api.service.mysql.BannedUserInfoService;
import com.cdks.wuxiu.api.service.mysql.DailySystemInfoService;
import com.cdks.wuxiu.api.service.mysql.UserBaseInfoService;
import com.cdks.wuxiu.api.service.mysql.UserLoginInfoService;
import com.cdks.wuxiu.api.service.mysql.UserStatisInfoService;
import com.cdks.wuxiu.api.service.mysql.UserWalletInfoService;
import com.cdks.wuxiu.api.util.TimeUtils;
import com.cdks.wuxiu.common.util.ServletUtils;
import com.cdks.wuxiu.entity.mysql.BannedUserInfo;
import com.cdks.wuxiu.entity.mysql.UserBaseInfo;
import com.cdks.wuxiu.entity.mysql.UserLoginInfo;

import cn.hutool.core.util.RandomUtil;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;


/**
 * 账号服务实现类
 */
@Service
public class AccountServiceImpl implements AccountService {
    @Resource
    private UserBaseInfoService userBaseInfoService;    
    @Resource
    private UserWalletInfoService userWalletInfoService;
    @Resource
    private UserLoginInfoService userLoginInfoService;
    @Resource
    private UserStatisInfoService userStatisInfoService; 
    @Resource
    private BannedUserInfoService bannedUserInfoService;
    @Resource
    private DailySystemInfoService dailySystemInfoService;


    @Override
    public UserBaseInfo userRegister(String phone, int branch, String udid) {
        // 创建用户
        UserBaseInfo userInfo  = new UserBaseInfo();
        userInfo.setType(Constant.ACCOUNT_TYPE_USER);
        userInfo.setPhone(phone);
        userInfo.setBranch(branch);
        userInfo.setNickName("舞秀" + RandomUtil.randomInt(1000, 9999));
        userInfo.setCreateTime(TimeUtils.getTimeSecond());
        userInfo.setUdid(udid);

        if (phone.equals("10000000110"))
        {
            userInfo.setType(Constant.ACCOUNT_TYPE_AUDT);
        }
        
        boolean ret  = userBaseInfoService.save(userInfo);
        if (ret == false)
        {
            return null;
        }

        // 初始化钱包
        userWalletInfoService.init(userInfo.getUserId());
        // 初始化用户统计信息
        userStatisInfoService.init(userInfo.getUserId());
        
        dailySystemInfoService.addRegisterCount();

        return userInfo;
    }

    @Override
    public BannedUserInfo checkAccount(long userId, String udid, String phone) {
        List<BannedUserInfo> bannedList  = bannedUserInfoService.getList(userId, udid, phone, ServletUtils.getIpAddress());
        if (bannedList.size() == 0)
        {
            return null;
        }

        for (BannedUserInfo bannedInfo : bannedList)
        {
            //该类型已解封继续检查下一类型
            if (bannedInfo.getType() == Constant.BANNED_TYPE_REMOVE)
            {
                continue;
            }

            //永久封禁
            if (bannedInfo.getExpire() == 0)
            {
                return bannedInfo;
            }

            //解封时间未到
            if (bannedInfo.getExpire() > TimeUtils.getTimeSecond())
            {
                return bannedInfo;
            }

            //如果是封ID到期自动解封
            if (bannedInfo.getType() == Constant.BANNED_TYPE_USERID)
            {
                userBaseInfoService.updateStatus(userId, Constant.ACCOUNT_STATUS_NORMAL);

                bannedInfo.setType(Constant.BANNED_TYPE_REMOVE);
                bannedUserInfoService.updateInfoById(bannedInfo);
            }
        }

        return null;
    }

    @Override
    public BannedUserInfo checkAccount(String udid, String phone) {
        return checkAccount(0, udid, phone);
    }

    @Override
    public BannedUserInfo checkAccount(long userId, String udid) {
        return checkAccount(userId, udid, null);
    }

    @Override
    public int checkAccount(long userId) {
        BannedUserInfo info  = checkAccount(userId, null, null);
        if (info == null)
        {
            return 0;
        }
        
        return -1;
    }

    @Override
    public int deleteAccount(UserBaseInfo userInfo) {
        userBaseInfoService.deleteUserInfo(userInfo);
        
        return 0;
    }

    @Override
    public int saveLoginInfo(UserLoginInfo loginInfo, boolean isNew) {
        if (isNew)
        {
            return userLoginInfoService.insertLoginInfo(loginInfo);
        }
        else
        {
            return userLoginInfoService.updateLoginInfo(loginInfo);
        }
    }

    @Override
    public UserLoginInfo getLoginInfo(long userId) {
        return userLoginInfoService.getByUserId(userId);
    }

    @Override
    public int updateDeviceInfo(long userId, String udid) {
        UserLoginInfo loginInfo  = new UserLoginInfo();
        loginInfo.setUserId(userId);
        loginInfo.setUdid(udid);
        
        return userLoginInfoService.updateLoginInfo(loginInfo);
    }
}
